package com.example.androidtest;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class MainActivity extends Activity implements OnClickListener{

    ImageButton btnplay, btnstop, btnpause;  
    SurfaceView surfaceView;  
    MediaPlayer mediaPlayer;  
    int position;  
    private String path = "http://www.jsharkey.org/downloads/dailytest.3gp"; 
  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);    
        btnplay=(ImageButton)this.findViewById(R.id.btnplay);  
        btnstop=(ImageButton)this.findViewById(R.id.btnplay);  
        btnpause=(ImageButton)this.findViewById(R.id.btnplay);  
          
        btnstop.setOnClickListener(this);  
        btnplay.setOnClickListener(this);  
        btnpause.setOnClickListener(this);  
          
        mediaPlayer=new MediaPlayer();  
        surfaceView=(SurfaceView) this.findViewById(R.id.surfaceView);  
      
        //设置SurfaceView自己不管理的缓冲区  
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);         
        surfaceView.getHolder().addCallback(new Callback() {          
            @Override  
            public void surfaceDestroyed(SurfaceHolder holder) {  
          
            }  
          
            @Override  
            public void surfaceCreated(SurfaceHolder holder) {  
                if (position>0) {  
                    try {  
                        //开始播放  
                        play();  
                        //并直接从指定位置开始播放  
                        mediaPlayer.seekTo(position);  
                        position=0;                       
                    } catch (Exception e) {  
                        // TODO: handle exception  
                    }  
                }  
            }             
            @Override  
            public void surfaceChanged(SurfaceHolder holder, int format, int width,  
                    int height) {  
  
            }  
        });       
    }  
    @Override  
    public void onClick(View v) {     
        switch (v.getId()) {  
        case R.id.btnplay:  
            play();  
            break;  
              
        case R.id.btnpause:  
            if (mediaPlayer.isPlaying()) {  
                mediaPlayer.pause();  
            }else{  
                mediaPlayer.start();  
            }  
            break;  
  
        case R.id.btnstop:  
            if (mediaPlayer.isPlaying()) {  
                mediaPlayer.stop();  
            }  
              
            break;  
        default:  
            break;  
        }  
  
    }  
    @Override  
    protected void onPause() {    
        //先判断是否正在播放  
        if (mediaPlayer.isPlaying()) {  
            //如果正在播放我们就先保存这个播放位置  
            position=mediaPlayer.getCurrentPosition()  
                    ;  
            mediaPlayer.stop();  
        }  
        super.onPause();  
    }  
  
    private void play() {  
    	Log.d("tag", "play");
        try {  
            mediaPlayer.reset();      
            mediaPlayer  
            .setAudioStreamType(AudioManager.STREAM_MUSIC);  
            mediaPlayer.setOnPreparedListener(onPreparedListener);
            //设置需要播放的视频  
            //mediaPlayer.setDataSource("/mnt/sdcard/movie.3gp");  
            mediaPlayer.setDataSource(path);
            //把视频画面输出到SurfaceView  
            mediaPlayer.setDisplay(surfaceView.getHolder());  
            mediaPlayer.prepareAsync();  
            //播放  
            //mediaPlayer.start();          
        } catch (Exception e) {  
            // TODO: handle exception  
        }  
    }  
    
    OnPreparedListener	onPreparedListener	=	new OnPreparedListener() {
		
		@Override
		public void onPrepared(MediaPlayer mp) {
			// TODO Auto-generated method stub
			Log.d("tag", "onPrepared");
			handler.sendEmptyMessage(0);
		}
	};
	
	Handler	handler	=	new	Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			mediaPlayer.start();
		}	
	};
    
}
